<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<!--
	Nonzero1.0 by nodethirtythree design
	http://www.nodethirtythree.com
	missing in a maze
-->

<html>
    <head>
        <title>MediConflict | Home</title>

        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
        <meta name="description" content="description"/>
        <meta name="keywords" content="keywords"/> 
        <meta name="author" content="author"/> 

        <!--<script type="text/javascript" src="JavaScript/jquery-1.4.4.min.js"></script>-->
        <script type="text/javascript" src="JavaScript/jquery-1.6.min.js"></script>
        <script type="text/javascript" src="JavaScript/jquery-ui-1.8.11/jquery-ui-1.8.11.custom.min.js"></script>

        <link rel="stylesheet" type="text/css" href="style.css" />
        <link rel="stylesheet" type="text/css" href="JavaScript/jquery-ui-1.8.11/css/redmond/jquery-ui-1.8.11.custom.css" />
    
        <script type="text/javascript"><!--
            $(document).ready(function ()
            {
                $("#userList").selectable();

                $("#accordion").accordion(
                {
                    collapsible: true,
                    active: -1
                });

                $("#logout").button().click(function ()
                {
                    SetCookie("username", "", -1);
                    SetCookie("password", "", -1);
                    SetCookie("selectedUser", "", -1);
                    $("#logout").hide();
                    $("#username").val("");
                    $("#password").val("");
                    $("#loginForm").dialog("open");
                    $("#accordion").accordion("option", "active", -1);
                });

                $("#userSelectionForm").dialog({
                    autoOpen: false,
                    height: 225,
                    width: 300,
                    modal: true,
                    resizable: false,
                    closeOnEscape: false,
                    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
                    buttons: {
                        "Show Schedule": function ()
                        {
                            var selectedUser;
                            var num = 0;
                            $(".ui-selected", $("#userList")).each(function ()
                            {
                                num++;
                                selectedUser = $("#userList li").text(this);
                            });

                            if (num == 0)
                            {
                                alert("Please select a user to display");
                            }
                            else if (num > 1)
                            {
                                alert("Only one user can be selected");
                            }
                            else
                            {
                                SetCookie("selectedUser", selectedUser, 1);

                                $("#userSelectionForm").dialog("close");

                                var username = ReadCookie("username");
                                var password = ReadCookie("password");

                                // Get schedule for selected user
                                $.ajax(
                                {
                                    type: "POST",
                                    url: "MediConflict",
                                    dataType: "xml",
                                    data: "loginName=" + username + "&password=" + password + "&userName=" + selectedUser,
                                    success: fillSchedule,
                                    error: AjaxFailed
                                });
                            }
                        }
                    }
                });

                $("#loginForm").dialog({
                    autoOpen: false,
                    height: 225,
                    width: 300,
                    modal: true,
                    resizable: false,
                    closeOnEscape: false,
                    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
                    buttons: {
                        "Login": doLogin
                    }
                });

                $('#loginForm').keypress(function (e)
                {
                    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
                    {
                        doLogin();
                        return false;
                    }
                });

                function doLogin()
                {
                    if ($("#username").val() == "")
                        alert("Please provide your username");
                    else if ($("#password").val() == "")
                        alert("Please provide your password");
                    else
                    {
                        var username = $("#username").val();
                        var password = $("#password").val();

                        $.ajax(
                        {
                            type: "POST",
                            url: "Login",
                            dataType: "xml",
                            data: "loginName=" + username + "&password=" + password,
                            success: function (xml)
                            {
                                var response = $.trim($(xml).find("response").text());
                                if (response == "error")
                                {
                                    alert("Username and/or password were invalid");
                                    $("#username").val("");
                                    $("#password").val("");
                                }
                                else
                                {
                                    SetCookie("username", username, 1);
                                    SetCookie("password", password, 1);

                                    alert("Welcome " + username);
                                    $("#logout").button("option", "label", "Logout");
                                    $("#logout").show();
                                    $("#loginForm").dialog("close");

                                    var userList = response.split(",");

                                    $("#userList").empty();
                                    for (var i = 0; i < userList.length; i++)
                                    {
                                        $("#userList").append("<li class='ui-widget-content'>" + userList[i] + "</li>");
                                    }

                                    $("#userSelectionForm").dialog("open");
                                }
                            },
                            error: AjaxFailed
                        });
                    }
                }

                var username = ReadCookie("username");
                if (username == null || username == "")
                    $("#loginForm").dialog("open");
                else
                {
                    $("#logout").button("option", "label", "Logout");
                    $("#logout").show();
                }
            });

            function fillSchedule(xml)
            {
                //$("#Thursday").empty();

                $(xml).find("daylist").each(function ()
                {
                    var day = $(this).attr("name");
                    //alert(day);

                    $("#" + day).empty();
                    $("#" + day).append($.trim($(this).find("dosage").text()));
                });

                //$("#Thursday").append("<p>" + $(xml).find("response").text() + "</p>");
                //$("#Thursday").append("<p>" + $(xml).find("response").text() + "</p>");
                //$("#Thursday").append("<p>" + $(xml).find("response").text() + "</p>");
                //$("#Thursday").append("<p>" + $(xml).find("response").text() + "</p>");
                //$("#Thursday").append("<p>" + $(xml).find("response").text() + "</p>");
				/*
                $(this).find("dosage").each(function ()
                {
	                //display info about each dosage
	                var id = $(this).attr("id");
	                var medicine = $.trim($(this).find("medicine").text());
	                var day = $.trim($(this).find("time").find("day").text());
	                var hour = $.trim($(this).find("time").find("hour").text()); ;
	                var minute = $.trim($(this).find("time").find("minute").text()); ;
	                var doseAmount = $.trim($(this).find("amount").text());
	                var doseUnit = $.trim($(this).find("unit").text());
                });
                */

                $("#accordion").accordion("option", "active", new Date().getDay());
                $("#Thursday").height(100);
            }

            function AjaxFailed(result)
            {
                alert(result.status + " " + result.statusText);
            }

            function SetCookie(cookieName, cookieValue, nDays) {
                var today = new Date();
                var expire = new Date();
                if (nDays == null || nDays == 0) nDays = 1;
                expire.setTime(today.getTime() + 3600000 * 24 * nDays);
                document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
            }

            function ReadCookie(cookieName) {
                var theCookie = "" + document.cookie;
                var ind = theCookie.indexOf(cookieName);
                if (ind == -1 || cookieName == "")
                    return "";
                var ind1 = theCookie.indexOf(';', ind);
                if (ind1 == -1)
                    ind1 = theCookie.length;
                return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
            }
        --></script>

        <style type="text/css">
            a.customBtn, a.customBtn:link, a.customBtn:visited
		    {
			    display:block;
			    color:#FFFFFF;
			    background-color:#2797D0;
			    font-weight:bold;
			    font-size:11px;
			    width:80px;
			    text-align:center;
			    padding-top:3px;
			    padding-bottom:4px;
			    /*padding-top:0.5px;
			    padding-bottom:1px;*/
			    border:1px solid #ffffff;
			    outline:1px solid #1D7DAF;
			    text-decoration:none;
			    margin-left:1px;
                padding-left: 0;
                padding-right: 0;
                cursor: pointer;
            }

		    a.customBtn:hover, a.customBtn:active
		    {
			    background-color:#1D7DAF;
		    }
		    
		    /*body { font-size: 62.5%; }*/
		    body { font-size: 70%; }
		    label, input { display:block; }
		    input.text { margin-bottom:12px; width:95%; padding: .4em; }
		    h1 { font-size: 1.2em; margin: .6em 0; }
		    div#users-contain { width: 350px; margin: 20px 0; }
		    div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
		    div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
		    .ui-dialog .ui-state-error { padding: .3em; }
		    .validateTips { border: 1px solid transparent; padding: 0.3em; }
		    #userList .ui-selecting { background: #D7DEE4; } /* light blue */
	        #userList .ui-selected { background: #70A8D2; color: white; } /* dark blue */
	        #userList { list-style-type: none; margin: 0; padding: 0; width: 100%; }
	        #userList li { margin: 3px; padding: 0.4em; font-size: 1.2em; height: 15px; }
        </style>
    </head>
    <body>
        <div id="header">
	        <div id="header_inner" class="fixed">
                <div style="float:right; color: #fff; padding-top: 55px; font-weight: bold;"><button id="logout" style="display:none;"></button></div>
		        <div id="logo">
			        <h1><span>MediConflict</span></h1>
		        </div>
	        </div>
        </div>
        <div id="main">
	        <div id="main_inner" class="fixed">
		        <div id="primaryContent_columnless">
			        <div id="columnA_columnless">
                        <div id="accordion">
	                        <h2><a>Sunday</a></h2>
	                        <div id="Sunday">

	                        </div>
	                        <h2><a>Monday</a></h2>
	                        <div id="Monday">

	                        </div>
	                        <h2><a>Tuesday</a></h2>
	                        <div id="Tuesday">
		                        
	                        </div>
	                        <h2><a>Wednesday</a></h2>
	                        <div id="Wednesday">

	                        </div>
                            <h2><a>Thursday</a></h2>
	                        <div id="Thursday">

	                        </div>
                            <h2><a>Friday</a></h2>
	                        <div id="Friday">

	                        </div>
                            <h2><a>Saturday</a></h2>
	                        <div id="Saturday">

	                        </div>
                        </div>

                        <div id="loginForm" title="Login">
                            <br />
		                    <label for="username" style="font-weight:bold;">Username:</label>
		                    <input type="text" name="username" id="username" class="text ui-widget-content ui-corner-all" />
		                    <label for="password" style="font-weight:bold;">Password:</label>
		                    <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
                        </div>

                        <div id="userSelectionForm" title="Select User">
                            <br />
                            <label for="userList" style="font-weight:bold;">Users:</label>
                            <ol id="userList">

                            </ol>
                        </div>
                    </div>
		        </div>
	        </div>
        </div>
        <br />

        <div id="footer" class="fixed">
	        Copyright &copy; 2011 Team Maverick. All Rights Reserved. Design By: <a href="http://www.nodethirtythree.com/" target="_blank">NodeThirtyThree Design</a>.
        </div>
    </body>
</html>